//
//  HMNewFeatureCell.swift
//  HMWeibo12
//
//  Created by iOS on 16/9/19.
//  Copyright © 2016年 iOS. All rights reserved.
//

import UIKit

class HMNewFeatureCell: UICollectionViewCell {
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// cell的indexPath
    var indexPath: IndexPath? {
        didSet {
            print("indexPath = \(indexPath)")
            backgroundImageView.image = UIImage(named: "new_feature_\(indexPath!.item + 1)")
            
            // 判断是最后一页才显示,其他页隐藏
            if indexPath?.item == NewFeatureCount - 1 {
                showButton(isHidden: false)
            } else {
                // 不是最后一页
                showButton(isHidden: true)
            }
        }
    }
    
    private func showButton(isHidden: Bool) {
        sharedButton.isHidden = isHidden
        startButton.isHidden = isHidden
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 对于 UICollectionViewCell 来说,自己添加的子控件都添加到contentView中
        self.contentView.addSubview(backgroundImageView)
        self.contentView.addSubview(sharedButton)
        self.contentView.addSubview(startButton)
        
        // 设置约束
        backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
        sharedButton.translatesAutoresizingMaskIntoConstraints = false
        startButton.translatesAutoresizingMaskIntoConstraints = false

        // 背景, 填充父控件 -> 4边和父控件重合
        // leading
        self.contentView.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: .leading, relatedBy: .equal, toItem: self.contentView, attribute: .leading, multiplier: 1, constant: 0))
        // top
        self.contentView.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: .top, relatedBy: .equal, toItem: self.contentView, attribute: .top, multiplier: 1, constant: 0))
        // trailing
        self.contentView.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: .trailing, relatedBy: .equal, toItem: self.contentView, attribute: .trailing, multiplier: 1, constant: 0))
        // bottom
        self.contentView.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: .bottom, relatedBy: .equal, toItem: self.contentView, attribute: .bottom, multiplier: 1, constant: 0))
        
        // 开始体验按钮
        // centerX
        self.contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: .centerX, relatedBy: .equal, toItem: self.contentView, attribute: .centerX, multiplier: 1, constant: 0))
        // bottom
        self.contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: .bottom, relatedBy: .equal, toItem: self.contentView, attribute: .bottom, multiplier: 1, constant: -130))
        
        // 分享按钮
        // centerX
        self.contentView.addConstraint(NSLayoutConstraint(item: sharedButton, attribute: .centerX, relatedBy: .equal, toItem: self.contentView, attribute: .centerX, multiplier: 1, constant: 0))
        // bottom
        self.contentView.addConstraint(NSLayoutConstraint(item: sharedButton, attribute: .bottom, relatedBy: .equal, toItem: startButton, attribute: .top, multiplier: 1, constant: -16))
    }
    
    // MARK: - 按钮点击事件
    func sharedButtonDidClick() -> Void {
        sharedButton.isSelected = !sharedButton.isSelected
    }
    
    func startButtonDidClick() -> Void {
        // 切换到 HMMainTabbarController
        UIApplication.shared.keyWindow?.rootViewController = HMMainTabBarController()
    }
    
    // MARK: - 懒加载
    /// 背景
    private lazy var backgroundImageView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    /// 分享按钮
    private lazy var sharedButton: UIButton = {
        let button = UIButton()
        
        // 设置
        // 文字
        button.setTitle("分享给大家", for: .normal)
        button.setTitleColor(UIColor.black, for: .normal)
        
        // 图片
        button.setImage(UIImage(named: "new_feature_share_false"), for: .normal)
        button.setImage(UIImage(named: "new_feature_share_true"), for: .selected)
        
        button.addTarget(self, action: #selector(sharedButtonDidClick), for: .touchUpInside)
        
        return button
    }()
    
    /// 开始按钮
    private lazy var startButton: UIButton = {
        let button = UIButton()
        
        // 设置
        // 文字
        button.setTitle("开始体验", for: .normal)
        
        // 图片
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), for: .normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), for: .highlighted)
        
        button.addTarget(self, action: #selector(startButtonDidClick), for: .touchUpInside)
        
        return button
    }()
}
